﻿using AutoMapper;
using B.S.DataBasic.Domain.GoodInfra;
using B.S.DataBasic.ErrorCode;
using B.S.DataBasic.Infrastructure.Implement;
using B.S.DataBasic.Write.API.Dtos;
using MediatR;

namespace B.S.DataBasic.Write.API.Application
{
    public class AddTraceCommandHandler : IRequestHandler<AddTraceCommand, APIResult<Traceabilitable>>
    {
        private readonly IBaseRepository<Traceabilitable> tanceRepository;
        private readonly IMapper mapper;

        public AddTraceCommandHandler(IBaseRepository<Traceabilitable> tanceRepository, IMapper mapper)
        {
            this.tanceRepository = tanceRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<Traceabilitable>> Handle(AddTraceCommand request, CancellationToken cancellationToken)
        {
            APIResult<Traceabilitable> result = new APIResult<Traceabilitable>();
            result.Code = EnumCode.添加成功;
            result.Message = EnumCode.添加成功.ToString();

            var repose = mapper.Map<Traceabilitable>(request);
            tanceRepository.AddAsync(repose);

            return await Task.FromResult(result);
        }


    }
}
